package com.software.loginregister.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.software.loginregister.R;
import com.software.loginregister.activity.DraftsActivity;
import com.software.loginregister.activity.NotificationActivity;
import com.software.loginregister.activity.SearchActivity;
import com.software.loginregister.adapter.HomeAdapter;

public class HomeFragment extends Fragment {
    private HomeAdapter homeAdapter;
    private ImageButton btnNotification;
    private ViewPager2 vpHomes;
    private ImageButton btnSearch;
    private TextView tvFridge;
    private TextView tvRecommend;

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);



        // 1. 初始化所有视图

        initViews(view);



        // 2. 设置状态栏

        if (getActivity() != null) {
            Window window = getActivity().getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            View decorView = window.getDecorView();
            int flags = decorView.getSystemUiVisibility();
            flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            decorView.setSystemUiVisibility(flags);
        }

        // 3. 初始化事件和适配器
        initEvents();
        initAdapters();
        return view;
    }

    private void initAdapters() {
        // 设置ViewPager适配器
        if (homeAdapter == null) {
            homeAdapter = new HomeAdapter(this);
        }
        vpHomes.setAdapter(homeAdapter);

        // 设置ViewPager页面切换监听
        vpHomes.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                updateTabSelection(position);
            }
        });

        // 设置标签点击事件
        tvFridge.setOnClickListener(v -> vpHomes.setCurrentItem(0));
        tvRecommend.setOnClickListener(v -> vpHomes.setCurrentItem(1));

        // 默认选中推荐页面
        vpHomes.post(() -> {
            vpHomes.setCurrentItem(1, false);  // 设置默认显示推荐页面
            updateTabSelection(1);  // 更新标签选中状态
        });
    }

    private void updateTabSelection(int position) {
        View indicatorFridge = getView().findViewById(R.id.indicator_fridge);
        View indicatorRecommend = getView().findViewById(R.id.indicator_recommend);
        // 重置所有标签颜色
        tvFridge.setTextColor(Color.parseColor("#666666"));
        tvRecommend.setTextColor(Color.parseColor("#666666"));
        indicatorFridge.setVisibility(View.INVISIBLE);
        indicatorRecommend.setVisibility(View.INVISIBLE);
        // 设置选中标签样式
        switch (position) {
            case 0:
                tvFridge.setTextColor(Color.parseColor("#000000"));
                indicatorFridge.setVisibility(View.VISIBLE);
                break;
            case 1:
                tvRecommend.setTextColor(Color.parseColor("#000000"));
                indicatorRecommend.setVisibility(View.VISIBLE);
                break;
        }
    }
    private void initEvents() {
        // 搜索框点击事件
        btnSearch.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), SearchActivity.class);
            startActivity(intent);
        });

        // 消息按钮点击事件
        btnNotification.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), NotificationActivity.class);
            startActivity(intent);
        });
    }

    private void initViews(View view) {
        btnNotification = view.findViewById(R.id.btn_notification);
        btnSearch = view.findViewById(R.id.btn_search);
        tvFridge = view.findViewById(R.id.tv_fridge);
        tvRecommend = view.findViewById(R.id.tv_recommend);
        vpHomes = view.findViewById(R.id.vp_homes);

        // 设置ViewPager2的缓存
        if (vpHomes != null) {
            vpHomes.setOffscreenPageLimit(1);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (vpHomes != null && homeAdapter != null) {
            // 如果是第一次加载，设置为推荐页面
            if (vpHomes.getCurrentItem() == 0) {
                vpHomes.setCurrentItem(1, false);
                updateTabSelection(1);
            }
            // 刷新当前页面
            int currentItem = vpHomes.getCurrentItem();
            homeAdapter.notifyItemChanged(currentItem);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 清理资源
        if (vpHomes != null) {
            vpHomes.setAdapter(null);
        }
        homeAdapter = null;
    }
}